package com.ruimei.service.impl;

import com.ruimei.entitys.Leave;
import com.ruimei.entitys.Parent;
import com.ruimei.entitys.Teacher;
import com.ruimei.entitys.enums.LeaveStatus;
import com.ruimei.mapper.LeaveMapper;
import com.ruimei.service.LeaveService;
import com.ruimei.util.CommUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.ruimei.util.JSONUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class LeaveServiceImpl
  implements LeaveService {
  @Resource
  LeaveMapper leaveMapper;

  @Resource
  RedisTemplate<String,String> redisTemplate;


  @Override
  public Map<String, Object> getLeaveByTeacherId(String leaveId, String classId) {
    List<Leave> leaves = this.leaveMapper.getLeaveByTeacherId(leaveId, classId);

    if (leaveId != null && leaves.get(0).getStatus() == LeaveStatus.PROCESSING) {
      this.leaveMapper.setPROCESSED(leaveId);
      leaves.set(0, this.leaveMapper.getLeaveByLeaveId(leaveId));
    }
    TimeOut(leaves);
    Object map = new HashMap();
    ((Map) map).put("status", Boolean.valueOf(true));
    ((Map) map).put("message", "获取成功");
    ((Map) map).put("count", Integer.valueOf(leaves.size()));
    ((Map) map).put("data", leaves);

    return (Map<String, Object>) map;
  }
  @Override
  public Map<String, Object> setLeave(Leave leave, Teacher teacher) {
    int i = this.leaveMapper.setLeave(leave.getRemarks(), leave.getLeaveId(), teacher.getFullName(), teacher.getId());

    Map<String, Object> map = new HashMap();
    if (i == 1) {
      map.put("status", Boolean.valueOf(true));
      map.put("message", "回复成功");
      return map;
    }
    map.put("status", Boolean.valueOf(false));
    map.put("message", "回复失败");
    return map;
  }
 @Override
  public boolean leaveParent(Leave leave,String parentId) {
     Parent parent  = null;
    if(redisTemplate.hasKey(parentId)){
      parent = (Parent) JSONUtils.jsonStringToBean(redisTemplate.opsForValue().get(parentId),Parent.class);
    }
    leave.setCreateUserName(parent.getFullName());
    leave.setCreateUserId(parent.getId());
    if (this.leaveMapper.insertLeave(leave) >= 1) {
      return true;
    }
    return false;
  }
  @Override
  public List<Leave> leaveView(String id, String leaveId) {
    List<Leave> leaves = this.leaveMapper.leaveView(id, leaveId);
    TimeOut(leaves);
    return leaves;
  }


  public List<Leave> TimeOut(List<Leave> leaves) {
    int index = 0;
    for (Leave leave : leaves) {
      Double nowTime = Double.valueOf(CommUtil.getStringDate().replaceAll("[-\\s:]", ""));
      Double endTime = Double.valueOf(leave.getLeaveEndTime().replaceAll("[-\\s:]", ""));
      if ((nowTime.doubleValue() >= endTime.doubleValue()) && (leave.getStatus() == LeaveStatus.PROCESSING)) {
        String id = leave.getLeaveId();

        this.leaveMapper.updateByLeaveId(id);

        leaves.set(index, this.leaveMapper.getLeaveByLeaveId(id));
      }
      index++;

    }
    return leaves;
  }
}
/* Location:              E:\360MoveData\Users\admin\Desktop\teacher_wap-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes\com\ruimei\service\impl\LeaveServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       0.7.1
 */